﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.NET.Application
{
    public static class PubHelper
    {
        /// <summary>
        /// 字符串转换为字节数组
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string hex)
        {
            int numberChars = hex.Length;
            byte[] bytes = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        /// <summary>
        /// 计算字节数组的校验和，进行Xor校验运算
        /// </summary>
        /// <param name="data">要计算校验和的字节数组</param>
        /// <returns>校验和</returns>
        public static byte CalculateCheckByteSum(byte[] data)
        {
            byte checksum = 0;
            foreach (byte value in data)
            {
                checksum ^= value;
            }
            return checksum;
        }

        ///// <summary>
        ///// 用于确保数组的长度符合预期，并在需要时在前面填充额外的空字节
        ///// </summary>
        ///// <param name="array"></param>
        ///// <param name="length"></param>
        ///// <returns></returns>
        //public static byte[] EnsureLength(byte[] array, int length)
        //{
        //    if (array.Length < length)
        //    {
        //        // 如果数组长度不足，填充额外的空字节
        //        byte[] newArray = new byte[length];
        //        Array.Copy(array, 0, newArray, length - array.Length, array.Length);
        //        return newArray;
        //    }
        //    else if (array.Length > length)
        //    {
        //        // 如果数组长度超过，截断至指定长度
        //        byte[] newArray = new byte[length];
        //        Array.Copy(array, array.Length - length, newArray, 0, length);
        //        return newArray;
        //    }
        //    else
        //    {
        //        // 数组长度符合预期，无需调整
        //        return array;
        //    }
        //}

        /// <summary>
        /// 用于确保数组的长度符合预期，并在需要时在后面填充额外的空字节
        /// </summary>
        /// <param name="array"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static byte[] EnsureLength(byte[] array, int length)
        {
            if (array.Length < length)
            {
                // 如果数组长度不足，填充额外的空字节在后面
                byte[] newArray = new byte[length];
                Array.Copy(array, 0, newArray, 0, array.Length);
                return newArray;
            }
            else if (array.Length > length)
            {
                // 如果数组长度超过，截断至指定长度
                byte[] newArray = new byte[length];
                Array.Copy(array, 0, newArray, 0, length);
                return newArray;
            }
            else
            {
                // 数组长度符合预期，无需调整
                return array;
            }
        }

    }
}
